﻿namespace SuperModel.BehaviorModel
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Extensions;

    /// <summary>
    /// A base class for property interception
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
    public abstract class PropertyInterceptionAttributeBase : Attribute
    {
        /// <summary>
        /// The filter types
        /// </summary>
        private IList<Type> filterTypes;

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyInterceptionAttributeBase"/> class.
        /// </summary>
        /// <param name="filters">
        /// The property set filter types.
        /// </param>
        protected PropertyInterceptionAttributeBase(params Type[] filters)
            : this()
        {
            FilterTypes = filters;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyInterceptionAttributeBase"/> class. 
        /// </summary>
        protected PropertyInterceptionAttributeBase()
        {
        }

        /// <summary>
        /// Gets or sets the Filter Types
        /// </summary>
        public Type[] FilterTypes
        {
            get { return filterTypes.ToArray(); }
            set
            {
                foreach (Type type in value)
                {
                    type.AssertImplements(PropertyFilterType);
                }

                filterTypes = new List<Type>(value);
            }
        }

        /// <summary>
        /// Gets Property Filter Type.
        /// </summary>
        protected abstract Type PropertyFilterType { get; }
    }
}
